#include "Char.h"
#include "../../../utils/instanceof.h"

SQLChar::~SQLChar()
{

}

SQLChar * SQLChar::instance()
{
	static SQLChar x;
	return &x;
}

Object_ptr SQLChar::eval(SQLEvalVisitor *visitor, SQLMethodInvokeExpr *x) {
	if (x->getParameters()->size() == 0) {
		return SQLEvalVisitor::EVAL_ERROR;
	}

	// StringBuffer buf = new StringBuffer(x.getParameters().size());
	std::stringstream buf;
	for (SQLExpr_ptr param : *x->getParameters()) {
		param->accept(SQLEvalVisitor_ptr(visitor));

		auto paramValue = param->getAttributes()->find(SQLEvalVisitor::EVAL_VALUE->c_str());

		if (paramValue->second->getType() == Object::ParamType::INT) {
			int charCode = paramValue->second->getInt();
			buf << charCode;
		} else if (paramValue->second->getType() == Object::ParamType::STRING) {
			// int charCode = new BigDecimal((String)paramValue->second).intValue();
			buf << paramValue->second->getString().c_str();
		} else {
			return SQLEvalVisitor::EVAL_ERROR;
		}
	}

	return Object_ptr(new Object(buf.str()));
}
